package com.lzx.iteam;

import android.app.Dialog;
import android.content.Intent;
import android.os.*;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.*;
import android.widget.*;
import com.lzx.iteam.adapter.EventChooseListAdapter;
import com.lzx.iteam.adapter.EventSendGridAdapter;
import com.lzx.iteam.adapter.EventsListAdapter;
import com.lzx.iteam.bean.EventListData;
import com.lzx.iteam.net.*;
import com.lzx.iteam.provider.CloudDBOperation;
import com.lzx.iteam.provider.EventsMsgDB;
import com.lzx.iteam.util.*;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shijunting on 15-5-19.
 * 事件首页
 * **回复的背景图 reply.9.png
 * **事件回复的数据库 EventMsgDB.java
 * **事件列表的数据库 与之前一致 CloudContactDB.java,CloudContactDBHelper.java,CloudDBOperation.java内修改
 */
public class EventsFragment extends Fragment implements View.OnClickListener, View.OnTouchListener, AbsListView.OnScrollListener {

    private MainActivity mActivity;
    private ImageView mCreateEvents;
    private TextView mHeaderText;
    private LinearLayout mHeaderLayout;
    private ProgressBar mHeaderProgress;
    private ListView mEventsList;
    private TextView mFooterText;
    private LinearLayout mFooterLayout;
    private ProgressBar mFooterProgress;
    private EventsListAdapter mEventAdapter;
    private EventChooseListAdapter mChooseAdapter;
    private EventSendGridAdapter mSendAdapter;
    private LinearLayout mEventNotice, mLayoutHelp;
    private ListView mLvChooseList;
    private GridView mGvSendGrid;
    private TextView mTvChooseBtn;

    private CloudDBOperation mCloudDBOperation;
    private EventsMsgDB mMsgDB;
    private List<EventListData> mFixModelData = new ArrayList<EventListData>();//固定的模板数据
    private ArrayList<String> catalogs = new ArrayList<String>();//顶部tab数据
    private ArrayList<EventListData> eventsListDatas = new ArrayList<EventListData>();
    private AllDialogUtil mDialogUtil;
    private Dialog mWaitDialog;
    private EventListCenter mCenter;
    private PreferenceUtil mPreferenceUtil;
    private Class<?>[] ACTIVITY = {null, null, null, null, ScheduleCalendarActivity.class, ApprovalActivity.class, AttendanceMainActivity.class, WeeklyMainActivity.class,SalaryMainActivity.class};


    private String mEventId;
    public static boolean isUpDate;// 是否需要读取数据库数据
    boolean is_scrolling = false;   // 是否还在滚动
    private int pageIndex = 1;      // 加载的页数
    private int mPosition;          // 长按条目记住的position
    private int mType = 0;          // 事件类型 0. 全部, 1：公告，2：审批，3：周报，4：普通事件，5：自定义  6:系统通知  7：活动 8：投票 9:会议通知（签到）

    private final int MSG_REMOVE_EVENT = 1002;
    private final int MSG_GET_NEW_EVENT = 1792;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_REMOVE_EVENT:
                    httpReqDlgDismiss();
                    if (msg.arg1 == 0) {
                        mCloudDBOperation.deleteEventByEventId(mEventId);
                        if (eventsListDatas.size() > mPosition) {
                            eventsListDatas.remove(mPosition);
                        }
                        //initEventsListData(pageIndex, true);
                        if (eventsListDatas.size() > 0) {
                            if (mEventAdapter != null) {
                                mEventAdapter.setData(eventsListDatas);
                            }
                        } else {
                            mEventsList.setVisibility(View.GONE);
                            mEventNotice.setVisibility(View.VISIBLE);
                        }

                    } else if (msg.arg1 == 7000) {
                        Toast.makeText(mActivity, (String) msg.obj, Toast.LENGTH_LONG).show();
                        mCloudDBOperation.deleteEventByEventId(mEventId);
                        initLocalEventsListData(mType);
                    } else {
                        if (msg.arg1 == AsynHttpClient.ERROR_LOCAL) {
                            Toast.makeText(mActivity, R.string.active_failure_no_network,
                                    Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(mActivity, (String) msg.obj, Toast.LENGTH_LONG).show();
                        }
                    }
                    break;
                case MSG_GET_NEW_EVENT:
                    if (msg.arg1 == 0) {
                        initLocalEventsListData(mType);
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (MainActivity) getActivity();
//        isUpDate = true;
        mCenter = EventListCenter.getInstance(mActivity);
        mPreferenceUtil = PreferenceUtil.getInstance(mActivity);
        mCloudDBOperation = new CloudDBOperation(mActivity);
        mMsgDB = new EventsMsgDB(mActivity);
        mDialogUtil = AllDialogUtil.getInstance(mActivity);
/*
        try {
            if (NetServiceManager.getInstance() != null) {
                Log.d("Service", "EventsFragment---addNewMessageInterface");
                NetServiceManager.getInstance().addNewMessageInterface(newMessageImpl);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
*/

    }

    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (rootView == null) {
            rootView = inflater.inflate(R.layout.events_frag_layout, container, false);
            initView(rootView);
        }
        // 缓存的rootView需要判断是否已经被加过parent，如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
//        initEventsListData(pageIndex);
        Log.d("Event&&&", "onCreateView");
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
//        if (isUpDate) {
//            initLocalEventsListData(mType);
//            isUpDate = false;
//        }
        initEventsListData(pageIndex);
    }

    /**
     * 只读取数据库
     *
     * @param position
     */
    private void initLocalEventsListData(final int position) {

        eventsListDatas.clear();
        eventsListDatas = (ArrayList) mCloudDBOperation.findEventsByType(position + "", pageIndex * 10);
        if (eventsListDatas.size() > 0) {
            mEventNotice.setVisibility(View.GONE);
            mEventsList.setVisibility(View.VISIBLE);
            if (mEventAdapter != null) {
                mEventAdapter.setData(eventsListDatas);
            }
            mActivity.eventIds.clear();
            for (EventListData eventsListData : mCloudDBOperation.findEventsByType("0")) {
                mActivity.eventIds.add(eventsListData.getEventId());
            }
            setMsgNotice();
        } else {
            mEventsList.setVisibility(View.GONE);
            mEventNotice.setVisibility(View.VISIBLE);
        }

    }

    /**
     * 先查数据库再走接口   切换类型查询时调用 pageIndex = 1
     *
     * @param pageIndex
     */
    private void initEventsListData(int pageIndex) {
        eventsListDatas.clear();
        eventsListDatas.addAll(mCenter.getEventData(pageIndex, mType));
        if (mEventAdapter == null) {
            mEventAdapter = new EventsListAdapter(mActivity);
            mEventsList.setAdapter(mEventAdapter);
        }
        mEventAdapter.setData(eventsListDatas);
        if (eventsListDatas.size() > 0) {
            mEventNotice.setVisibility(View.GONE);
            mEventsList.setVisibility(View.VISIBLE);
            mActivity.eventIds.clear();
            for (EventListData eventsListData : mCloudDBOperation.findEventsByType("0")) {
                mActivity.eventIds.add(eventsListData.getEventId());
            }
            setMsgNotice();
        } else {
            mEventNotice.setVisibility(View.VISIBLE);
            mEventsList.setVisibility(View.GONE);
        }

        mCenter.setUpdateEventListener(new EventListCenter.EventDataListener() {

            @Override
            public void onChange(ArrayList<EventListData> eventList) {
                if (eventList.size() > 0) {
                    mEventNotice.setVisibility(View.GONE);
                    mEventsList.setVisibility(View.VISIBLE);
                } else {
                    mEventNotice.setVisibility(View.VISIBLE);
                    mEventsList.setVisibility(View.GONE);
                }
                eventsListDatas.clear();
                eventsListDatas.addAll(eventList);
                mEventAdapter.setData(eventsListDatas);
                Log.d("UpDate_event_list", " 呵呵  更新了");
                if (mEventAdapter.getCount() == Integer.parseInt(mPreferenceUtil.getString("event_count", "0"))) {
                    setFooterText(true);
                }
                mEventsList.setHeaderDividersEnabled(true);
                mHeaderLayout.setVisibility(View.GONE);
            }

            @Override
            public void onNoChange(ArrayList<EventListData> eventList) {
                Log.d("UpDate_event_list", " 呵呵  更新了，但是没变化 ");
            }

            @Override
            public void onAddMore(ArrayList<EventListData> eventList) {
                eventsListDatas.clear();
                eventsListDatas.addAll(eventList);
                mEventAdapter.setData(eventsListDatas);
                Log.d("UpDate_event_list", " 呵呵  加载更多");
            }

            @Override
            public void onError(int errNum, String obj) {
                Log.d("UpDate_talk_list", " 呵呵  出错了 ");

                if (errNum == 1002 || errNum == 1003 || errNum == 1005){
                    backToLogin(obj);
                }else{
                    Toast.makeText(mActivity, obj, Toast.LENGTH_LONG).show();
                }

            }
        });
    }

    private void setMsgNotice() {
        int unread = mMsgDB.getUnReadCountOfAllEvent(mActivity.eventIds);
        if (unread == 0) {
            mActivity.mAllEventReplyUnReadMsg.setVisibility(View.GONE);
        } else {
            mActivity.mAllEventReplyUnReadMsg.setVisibility(View.VISIBLE);
            if (unread < 10) {
                ViewGroup.LayoutParams linearParams = mActivity.mAllEventReplyUnReadMsg.getLayoutParams();
                int pd = Constants.dip2px(mActivity, 20);
                linearParams.width = pd;
                linearParams.height = pd;
                mActivity.mAllEventReplyUnReadMsg.setLayoutParams(linearParams);
                mActivity.mAllEventReplyUnReadMsg.setBackgroundResource(R.drawable.unread_1);
                mActivity.mAllEventReplyUnReadMsg.setGravity(Gravity.CENTER);
                mActivity.mAllEventReplyUnReadMsg.setText(unread + "");
            } else if (unread < 100) {
                ViewGroup.LayoutParams linearParams = mActivity.mAllEventReplyUnReadMsg.getLayoutParams();
                int widthPx = Constants.dip2px(mActivity, 30);
                int heightPx = Constants.dip2px(mActivity, 20);
                linearParams.width = widthPx;
                linearParams.height = heightPx;
                mActivity.mAllEventReplyUnReadMsg.setLayoutParams(linearParams);
                mActivity.mAllEventReplyUnReadMsg.setBackgroundResource(R.drawable.unread_2);
                mActivity.mAllEventReplyUnReadMsg.setText(unread + "");
            } else if (unread >= 100) {
                ViewGroup.LayoutParams linearParams = mActivity.mAllEventReplyUnReadMsg.getLayoutParams();
                int widthPx = Constants.dip2px(mActivity, 40);
                int heightPx = Constants.dip2px(mActivity, 20);
                linearParams.width = widthPx;
                linearParams.height = heightPx;
                mActivity.mAllEventReplyUnReadMsg.setLayoutParams(linearParams);
                mActivity.mAllEventReplyUnReadMsg.setBackgroundResource(R.drawable.unread_3);
                mActivity.mAllEventReplyUnReadMsg.setText("99+");
            }
        }
    }


    private void initView(View view) {
        mEventNotice = (LinearLayout) view.findViewById(R.id.ll_no_data);
        mCreateEvents = (ImageView) view.findViewById(R.id.iv_create_events);
        mLvChooseList = (ListView) view.findViewById(R.id.lv_choose_list);
        mGvSendGrid = (GridView) view.findViewById(R.id.gv_send_grid);
        mTvChooseBtn = (TextView) view.findViewById(R.id.tv_choose_btn);
        mCreateEvents.setOnClickListener(this);
        mTvChooseBtn.setOnClickListener(this);
        mLayoutHelp = (LinearLayout) view.findViewById(R.id.event_ll_help);
        mLayoutHelp.setOnClickListener(this);
        mEventsList = (ListView) view.findViewById(R.id.lv_events_list);
        View footerView = LayoutInflater.from(mActivity).inflate(R.layout.xlistview_footer, null);
        mFooterLayout = (LinearLayout) footerView.findViewById(R.id.xlistview_footer_content);
        mFooterText = (TextView) footerView.findViewById(R.id.xlistview_footer_hint_textview);
        mFooterProgress = (ProgressBar) footerView.findViewById(R.id.xlistview_footer_progressbar);
        mFooterLayout.setVisibility(View.GONE);
        mEventsList.addFooterView(footerView);
        View headerView = LayoutInflater.from(mActivity).inflate(R.layout.xlistview_footer, null);
        mHeaderLayout = (LinearLayout) headerView.findViewById(R.id.xlistview_footer_content);
        mHeaderText = (TextView) headerView.findViewById(R.id.xlistview_footer_hint_textview);
        mHeaderProgress = (ProgressBar) headerView.findViewById(R.id.xlistview_footer_progressbar);
        mHeaderLayout.setVisibility(View.GONE);
        mHeaderText.setText("正在刷新");
        mEventsList.addHeaderView(headerView);
        mEventsList.setOnScrollListener(this);
        initTopData();
        mEventsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (Constants.isFastClick()) {
                    return;
                }
                if (position == mEventAdapter.getCount()+1||position==0) {
                    return;
                } else {
                    Intent intent = new Intent(mActivity, EventDetailContentActivity.class);
                    EventListData eventListData = (EventListData) mEventAdapter.getItem(position-1);
                    intent.putExtra("event_msg", eventListData);
                    EventDetailContentActivity.index = -1;
                    startActivity(intent);
                }
            }
        });
        mEventsList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == mEventAdapter.getCount()||position==0) {
                    return true;
                } else {
                    mPosition = position-1;
                    showDeleteEventDlg(((EventListData) mEventAdapter.getItem(position-1)).getEventId());
                    mEventId = ((EventListData) mEventAdapter.getItem(position-1)).getEventId();
                    return true;
                }
            }
        });


    }

    /**
     * 初始化筛选列表和创建列表数据
     */
    private void initTopData() {
        // TODO 左上筛选列表
        catalogs.add(getString(R.string.event_tab_all));
        catalogs.add(getString(R.string.event_tab_activity));
        catalogs.add(getString(R.string.event_tab_notice));
        catalogs.add(getString(R.string.event_tab_vote));
        catalogs.add(getString(R.string.event_tab_reg));
        catalogs.add(getString(R.string.event_tab_normal_event));

        mChooseAdapter = new EventChooseListAdapter(mActivity, catalogs);
        mLvChooseList.setAdapter(mChooseAdapter);
        mLvChooseList.setOnTouchListener(this);
        mLvChooseList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                switch (position) {
                    case 0:
                        update(0);
                        break;
                    case 1:
                        update(7);
                        break;
                    case 2:
                        update(1);
                        break;
                    case 3:
                        update(8);
                        break;
                    case 4:
                        update(9);
                        break;
                    case 5:
                        update(4);
                        break;
                }
                mChooseAdapter.setSelectedPosition(position);
                mLvChooseList.setVisibility(View.GONE);
            }
        });

        // TODO 右上创建列表
        String[] name = {"公告", "活动", "投票", "会议通知", "日程", "审批", "考勤签到", "周报", "工资计算","事件", "", ""};
        String[] type = {"1", "7", "8", "9", "", "", "", "","", "4", "", ""};
        ArrayList<Integer> head = new ArrayList<Integer>();
        head.add(R.drawable.send_public_normal);
        head.add(R.drawable.send_active_normal);
        head.add(R.drawable.send_vote_normal);
        head.add(R.drawable.send_reg_normal);
        head.add(R.drawable.send_schedule_normal);
        head.add(R.drawable.send_approval_normal);
        head.add(R.drawable.send_attendance_normal);
        head.add(R.drawable.send_weekly_iv);
        head.add(R.drawable.iv_salary_icon);
        head.add(R.drawable.send_event_normal);
        head.add(-1);
        head.add(-1);

        for (int i = 0; i < name.length; i++) {
            EventListData eventListData = new EventListData();
            eventListData.setEventName(name[i]);
            eventListData.setEventType(type[i]);
            eventListData.setEventTitleHead(head.get(i));
            mFixModelData.add(eventListData);
        }
        mSendAdapter = new EventSendGridAdapter(mActivity, mFixModelData);
        mGvSendGrid.setAdapter(mSendAdapter);
        mGvSendGrid.setOnTouchListener(this);
        mGvSendGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                EventListData eventData = null;
                switch (position) {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 9:
                        Intent event = new Intent(mActivity, CreateNewEventActivity.class);
                        Bundle bundle = new Bundle();
                        eventData = (EventListData) mSendAdapter.getItem(position);
                        bundle.putSerializable("event_bundle", eventData);
                        event.putExtra("event_msg", bundle);
                        startActivity(event);
                        break;
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                        Intent activity = new Intent(mActivity, ACTIVITY[position]);
                        startActivity(activity);
                        break;

                }
                mGvSendGrid.setVisibility(View.GONE);
                mEventsList.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void onClick(View v) {
        EventListData eventData = new EventListData();
        switch (v.getId()) {
            case R.id.iv_create_events:
                if (mGvSendGrid.getVisibility() == View.VISIBLE) {
                    mGvSendGrid.setVisibility(View.GONE);
                } else {
                    if (mLvChooseList.getVisibility() == View.VISIBLE) {
                        mLvChooseList.setVisibility(View.GONE);
                    }
                    mGvSendGrid.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.tv_choose_btn:
                if (mLvChooseList.getVisibility() == View.VISIBLE) {
                    mLvChooseList.setVisibility(View.GONE);
                } else {
                    if (mGvSendGrid.getVisibility() == View.VISIBLE) {
                        mGvSendGrid.setVisibility(View.GONE);
                    }

                    mLvChooseList.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.event_ll_help:
                Intent intent = new Intent(mActivity, WebViewActivity.class);
                intent.putExtra("title", "工作平台基本操作");
                intent.putExtra("type", 4);
                startActivity(intent);
                break;
        }
    }

    /**
     * 删除事件
     *
     * @param eventId 事件id
     */
    public void removeEvent(String eventId) {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
                .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_EVENT_ID, eventId));
        GetMsgHttpReceiver deleteEventMsg = new GetMsgHttpReceiver(mHandler.obtainMessage(MSG_REMOVE_EVENT));
        deleteEventMsg.mApi = AsynHttpClient.API_EVENT_REMOVE;
        deleteEventMsg.mParams = nvps;
        AsynHttpClient.getInstance(mActivity).execEventHttp(deleteEventMsg);
    }

    /**
     * 退出或解散群组
     */
    public void showDeleteEventDlg(final String eventId) {

        String okText = this.getString(R.string.label_btn_delete);
        String cancelText = this.getString(R.string.cancel);
        mDialogUtil.titleDeleteMsgStyle(okText, cancelText, "删除并退出此事件");
        mDialogUtil.setOnDialogListener(new AllDialogUtil.DialogOnClickListener() {
            @Override
            public void onConfirmClick(Dialog lDialog) {
                httpReqDlgShow();
                removeEvent(eventId);
                lDialog.dismiss();
            }

            @Override
            public void onCancelClick(Dialog lDialog) {
                lDialog.dismiss();
            }
        });
    }

    /**
     * 显示等待的dialog
     */
    private void httpReqDlgShow() {
        if (mWaitDialog == null) {
            mWaitDialog = AllDialogUtil.getInstance(mActivity).waitDialog();
//            mProgressDialog.setCancelable(false);
            mWaitDialog.show();
        }

    }

    /**
     * 取消等待的dialog
     */
    private void httpReqDlgDismiss() {
        if (mWaitDialog != null && mWaitDialog.isShowing()) {
            mWaitDialog.dismiss();
            mWaitDialog = null;
        }
    }

    /**
     * 更新此Fragment的方法
     */
    public void update(int position) {
        mType = position;
        pageIndex = 0;
        initEventsListData(++pageIndex);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (view.getId()) {
            case R.id.lv_choose_list:
                mLvChooseList.setVisibility(View.GONE);
                break;
            case R.id.gv_send_grid:
                mGvSendGrid.setVisibility(View.GONE);
                break;

        }
        return false;
    }


    /**
     * 获取新事件列表
     */
    public void getNewEventsList(String eventIds, Message callback) {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
                .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_EVENT_IDS, eventIds));
        MyMessage.GetNewEventListMessage createEventMsg = new MyMessage.GetNewEventListMessage(callback, mActivity, "-1");
        createEventMsg.mApi = AsynHttpClient.API_EVENT_LIST_BY_EVENTS;
        createEventMsg.mParams = nvps;
        AsynHttpClient.getInstance(mActivity).execEventHttp(createEventMsg);
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int scrollState) {
        switch (absListView.getId()) {
            case R.id.lv_events_list:
                if (AbsListView.OnScrollListener.SCROLL_STATE_IDLE == scrollState) {
                    if (is_scrolling) {
                        if (mEventAdapter.getCount() != Integer.parseInt(mPreferenceUtil.getString("event_count", "0"))) {

                            if (NetworkUtil.checkNetwork(getActivity())){
                                // TODO 直接从接口读取数据，不先读取数据库
                                mCenter.getEventsList(mType + "", ++pageIndex, false);
                            }else{
                                mHandler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(getActivity(), "请检查您的网络", Toast.LENGTH_SHORT).show();
                                        setFooterText(true);
                                    }
                                },1000);
                            }
                        } else {
                            setFooterText(true);
                        }
                    } else {
                        // 判断滚动到顶部
                        if (mEventsList.getFirstVisiblePosition() == 0) {
                            mEventsList.setHeaderDividersEnabled(false);
                            mHeaderLayout.setVisibility(View.VISIBLE);

                            if (NetworkUtil.checkNetwork(getActivity())){
                                pageIndex = 1;
                                mCenter.getEventsList(mType + "", pageIndex, false);
                            }else{
                                mHandler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(getActivity(), "请检查您的网络", Toast.LENGTH_SHORT).show();
                                        mEventsList.setHeaderDividersEnabled(true);
                                        mHeaderLayout.setVisibility(View.GONE);
                                    }
                                },1000);
                            }



                        }
                    }


                }
                break;
        }
    }

    @Override
    public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        switch (absListView.getId()) {
            case R.id.lv_events_list:
                if ((firstVisibleItem + visibleItemCount == totalItemCount) && totalItemCount != 0) {
                    if (mEventAdapter.getCount() == Integer.parseInt(mPreferenceUtil.getString("event_count", "0"))) {
                        setFooterText(true);
                    } else {
                        mFooterLayout.setVisibility(View.VISIBLE);
                    }
                    is_scrolling = true;
                } else {
                    is_scrolling = false;
                }
                break;
        }
    }

    public void setFooterText(boolean isFinish) {
        if (isFinish) {
            mFooterLayout.setVisibility(View.GONE);
        } else {
            mFooterText.setText(this.getResources().getString(
                    R.string.xlistview_footer_hint_normal));
            mFooterProgress.setVisibility(View.VISIBLE);
        }
    }


    /**
     * 跳转登录页面
     * @param messsge
     */
    private void backToLogin(String messsge){
        mDialogUtil.titleMsgBtnStyle("提示", messsge, mActivity.getString(R.string.ok));
        mDialogUtil.setOnDialogListener(new AllDialogUtil.DialogOnClickListener() {

            @Override
            public void onConfirmClick(Dialog lDialog) {
                lDialog.dismiss();
                Intent intent = new Intent(mActivity, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                mActivity.startActivity(intent);
                mActivity.finish();
            }

            @Override
            public void onCancelClick(Dialog lDialog) {
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
/*
        try {
            if (NetServiceManager.getInstance() != null) {
                Log.d("Service", "EventFragment---removeNewMessageInterface");
                NetServiceManager.getInstance().removeNewMessageInterface(newMessageImpl);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
*/

    }


}